<?php
namespace app\common\model;
use think\Model;
use think\Db;
class Csv {
	//导出
	/*
	 
	 * $str 导出csv表的头部
	 *$list 导出数据表里的字段,如果没值就传空值  $list="";
	 * $result导出内容
	 * $filename导出的文件名
	 *  */
public function dc($str,$list,$result,$filename){

	$str=$str.","."\n";
	$str = iconv('utf-8','gbk',$str);
		if($list!="" && $result!=""){
	$list= explode(',', $list);
        foreach($result as $k=>$row){
        	for($i=0;$i<count($list);$i++){
        		$p=$list[$i];
        		if($i==count($list)-1){
        			$str .= iconv('utf-8','gbk',$row[$p]).","."\n"; //中文转码 
        		}else{
        			$str .= iconv('utf-8','gbk',$row[$p]).","; //中文转码 
        		}
             }
            } 
		}
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;

     }
     /*
      
      * 
      *导入 
      * */
    public function dr($fileName){
    		move_uploaded_file($_FILES['myfile']['tmp_name'],$fileName);
			$hand = fopen("$fileName","r");
			$arr_sql=array();
			while ($data = fgetcsv ($hand, 1000, ",")) {
			$sql="";
				$num = count ($data);
					for ($c=0; $c < $num; $c++) {
						$data[$c] = mb_convert_encoding($data[$c], "UTF-8", "GBK");
						if($c==$num-1)
						{
							$sql=$sql.$data[$c];
							break;
						}
						$sql=$sql.$data[$c].",";
					}
				array_push($arr_sql,$sql);
			}
    	return $arr_sql;
    	
    } 
     
     
    }
?>